package com.bfsoft.springbootinit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bfsoft.springbootinit.mapper.SysFunctionMapper;
import com.bfsoft.springbootinit.pojo.dto.IdDTO;
import com.bfsoft.springbootinit.pojo.dto.function.PermissionDTO;
import com.bfsoft.springbootinit.pojo.entity.SysFunction;
import com.bfsoft.springbootinit.pojo.entity.SysRoleFunction;
import com.bfsoft.springbootinit.pojo.vo.ResultVO;
import com.bfsoft.springbootinit.pojo.vo.function.FunctionCheckedVO;
import com.bfsoft.springbootinit.pojo.vo.function.FunctionVO;
import com.bfsoft.springbootinit.service.ISysFunctionService;
import com.bfsoft.springbootinit.service.ISysRoleFunctionService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * (功能表)SYS_FUNCTION 服务实现类
 * </p>
 *
 * @author xxx
 * @since 2024-10-18
 */
@Service
public class SysFunctionServiceImpl extends ServiceImpl<SysFunctionMapper, SysFunction> implements ISysFunctionService {

    @Resource
    private ISysRoleFunctionService sysRoleFunctionService;


    @Override
    public ResultVO<FunctionCheckedVO> getListByRole(IdDTO dto) {
        FunctionCheckedVO functionCheckedVO = new FunctionCheckedVO();

        List<SysRoleFunction> sysRoleFunctions = sysRoleFunctionService.list(new LambdaQueryWrapper<SysRoleFunction>()
                .eq(SysRoleFunction::getRId, dto.getId()));
        functionCheckedVO.setCheckedList(sysRoleFunctions.stream().map(SysRoleFunction::getFId).collect(Collectors.toList()));

        List<SysFunction> parentFunctionList = list(new LambdaQueryWrapper<SysFunction>()
                .isNull(SysFunction::getParentCode)
                .orderByAsc(SysFunction::getSort));
        List<FunctionVO> functionVOS = new ArrayList<>();
        parentFunctionList.forEach(i -> {
            FunctionVO functionVO = new FunctionVO();
            BeanUtils.copyProperties(i, functionVO);
            functionVO.setChildren(getChildren(i.getId()));
            functionVOS.add(functionVO);
        });
        functionCheckedVO.setFunctionVOList(functionVOS);

        return ResultVO.ok(functionCheckedVO);
    }

    private List<FunctionVO> getChildren(String pid) {
        List<FunctionVO> functionVOS = list(new LambdaQueryWrapper<SysFunction>()
                .eq(SysFunction::getParentCode, pid)
                .orderByAsc(SysFunction::getSort))
                .stream().map(i -> {
                    FunctionVO functionVO = new FunctionVO();
                    BeanUtils.copyProperties(i, functionVO);
                    return functionVO;
                }).collect(Collectors.toList());
        functionVOS.forEach(i -> i.setChildren(getChildren(i.getId())));
        return functionVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVO<Object> setPermission(PermissionDTO dto) {
        List<SysRoleFunction> sysRoleFunctions = sysRoleFunctionService.list(new LambdaQueryWrapper<SysRoleFunction>()
                .eq(SysRoleFunction::getRId, dto.getRoleId()));
        if (!sysRoleFunctions.isEmpty()){
            //过滤出新增数据和删除数据
            List<String> newList = dto.getCheckedList().stream().filter(i -> !sysRoleFunctions.stream().map(SysRoleFunction::getFId).collect(Collectors.toList()).contains(i)).collect(Collectors.toList());
            List<String> delList = sysRoleFunctions.stream().map(SysRoleFunction::getFId).collect(Collectors.toList()).stream().filter(i -> !dto.getCheckedList().contains(i)).collect(Collectors.toList());
            newList.forEach(i -> {
                SysRoleFunction sysRoleFunction = new SysRoleFunction();
                sysRoleFunction.setFId(i);
                sysRoleFunction.setRId(dto.getRoleId());
                sysRoleFunctionService.save(sysRoleFunction);
            });
            delList.forEach(i -> sysRoleFunctionService.remove(new LambdaQueryWrapper<SysRoleFunction>().eq(SysRoleFunction::getRId,dto.getRoleId()).eq(SysRoleFunction::getFId,i)));
        }else {
            dto.getCheckedList().forEach(i -> {
                SysRoleFunction sysRoleFunction = new SysRoleFunction();
                sysRoleFunction.setFId(i);
                sysRoleFunction.setRId(dto.getRoleId());
                sysRoleFunctionService.save(sysRoleFunction);
            });
        }
        return ResultVO.ok(null, "设置成功");
    }


}
